module.exports = {
  // 成功提示
  apiSuccess(data = '', msg = 'ok', code = 200) {
    this.body = { msg, data };
    this.status = code;
  },
  // 失败提示
  apiFail(data = '', msg = 'fail', code = 400) {
    this.body = { msg, data };
    this.status = code;
  },
  getToken(value) {
    return this.app.jwt.sign(value, this.app.config.jwt.secret);
  },
  //分页
  async page(name, where = {}, options = {}) {
    this.validate({
      page:{type:'int',required:true,desc:'页码'},
      limit:{type:'int',desc:'每页条数'}
    })
    let page = this.params.page ? parseInt(this.params.page) : 1;
    let limit = this.query.limit ? parseInt(this.query.limit) : 10;
    if(this.query.type>=0){
      options.include=[{
        model: this.model.Category
      }]
    }
    let offset = (page - 1) * limit
    if (!options.order) {
      options.order = [
        ['id', 'DESC']
      ]
    }
    console.log(options)
    return await this.model[name].findAll({
      where,
      offset,
      limit,
      ...options
    })
  }

};